<?php

namespace App\HttpController\Admin\Auth;

use App\HttpController\Admin\BaseController;
use App\Logic\Auth\Form\UserListForm;
use App\Logic\Auth\Form\UserSaveForm;
use App\Logic\Auth\UserService;

use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

/**
 * Class User
 * @package App\HttpController\Api\Auth
 * @ApiGroup(groupName="权限-用户管理")
 * @ApiGroupDescription("权限-用户管理")
 */
class User extends BaseController{

    /**
     * @Api(name="列表",path="/admin/auth/user/index")
     * @Method(allow={GET})
     * @ApiDescription("列表")
     * @Param(name="name",description="姓名")
     * @Param(name="username",description="登录账号")
     * @Param(name="phone",description="手机号")
     * @Param(name="type",description="账号类型")
     * @Param(name="created_at_begin",description="创建时间-开始")
     * @Param(name="created_at_end",description="创建时间-结束")
     * @Param(name="merchants_id",description="商户id")
     * @Param(name="warehouse_id",description="仓库id")
     * @Param(name="role_id",description="角色id")
     * @Param(name="state",description="审核状态")
     * @ApiSuccessParam(name="id",description="spu id")
     * @ApiSuccessParam(name="role_id",description="角色id")
     * @ApiSuccessParam(name="username",description="登录账号")
     * @ApiSuccessParam(name="email",description="邮箱")
     * @ApiSuccessParam(name="name",description="姓名")
     * @ApiSuccessParam(name="phone",description="姓名")
     * @ApiSuccessParam(name="type",description="账号类型")
     * @ApiSuccessParam(name="state",description="用户状态 对照dict")
     * @ApiSuccessParam(name="type",description="账号类型 对照dict")
     * @ApiSuccessParam(name="last_login_time",description="最后登录时间")
     * @ApiSuccessParam(name="created_at",description="创建时间")
     * @ApiSuccessParam(name="merchants_id",description="商户id")
     * @ApiSuccessParam(name="merchants_name",description="商户")
     * @ApiSuccessParam(name="merchants_id",description="仓库id")
     * @ApiSuccessParam(name="warehouse_name",description="仓库")
     */
    public function index(){
        $params = $this->request()->getQueryParams();

        $listForm = new UserListForm($params, $this->request());

        $result = $listForm->lists();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="保存",path="/admin/auth/user/action")
     * @ApiDescription("保存")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="id",description="ID值【更新时必填】",type="integer")
     * @Param(name="role_id",description="角色id",required="",type="integer")
     * @Param(name="username",description="登录用户名",required="",type="integer")
     * @Param(name="psw",description="密码",required="",type="string")
     * @Param(name="name",description="姓名",required="",type="string")
     * @ApiSuccessParam(name="id",description="操作id",type="integer")
     */
    public function action(){
        $jsonData = $this->getJson();

        $saveForm = new UserSaveForm($jsonData);

        $result = $saveForm->saveData();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="字典",path="/admin/auth/user/dict")
     * @ApiDescription("字典")
     * @Method(allow={GET})
     * @ApiSuccessParam(name="type_list",description="类型",type="array")
     * @ApiSuccessParam(name="role_list",description="角色",type="array")
     * @ApiSuccessParam(name="merchants_list",description="商户",type="array")
     * @ApiSuccessParam(name="warehouse_list",description="仓库",type="array")
     */
    public function dict(){
        $result = UserService::getInstance()->getDict();
        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="设置显示",path="/admin/auth/user/settingState")
     * @ApiDescription("设置显示")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="ids",description="用户id",required="",type="array")
     * @Param(name="state",description="用户状态[0：禁用，1：正常]",required="",type="integer")
     * @ApiSuccessParam(name="affected_rows",description="操作条数",type="integer")
     */
    public function settingState(){
        $jsonData = $this->getJson();

        $result = UserService::getInstance()->settingState($jsonData);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="审核通过",path="/admin/auth/user/approved")
     * @ApiDescription("审核通过")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="ids",description="用户id",required="",type="array")
     * @ApiSuccessParam(name="affected_rows",description="操作条数",type="integer")
     */
    public function approved(){
        $jsonData = $this->getJson();

        $result = UserService::getInstance()->approved($jsonData);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="审核失败",path="/admin/auth/user/refuse")
     * @ApiDescription("审核失败")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="ids",description="用户id",required="",type="array")
     * @Param(name="examine_msg",description="审核失败内容",required="",type="string")
     * @ApiSuccessParam(name="affected_rows",description="操作条数",type="integer")
     */
    public function refuse(){
        $jsonData = $this->getJson();

        $result = UserService::getInstance()->refuse($jsonData);

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="重置密码",path="/admin/auth/user/resetPassword")
     * @ApiDescription("重置密码")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="id",description="用户id",required="",type="array")
     * @ApiSuccessParam(name="password",description="操作条数",type="integer")
     */
    public function resetPassword(){
        $jsonData = $this->getJson();

        $result = UserService::getInstance()->resetPassword($jsonData);

        return $this->writeJsonByOutput($result);
    }

}
